<template>
  <div v-loading="showLoading">
    <div v-if="type === 1">
      <div class="fx">
        <div class="block f5bg">
          <div class="block-left">近45日是否有开票记录：</div>
          <div
            class="block-right"
            v-html-no="{
              action: `${invoiceInformation.billingRecords ? '是' : '否'}`
            }"
          ></div>
        </div>
        <div class="block f5bg">
          <div class="block-left">近30天开票金额(元)：</div>
          <div
            class="block-right"
            v-html-no="{ action: invoiceInformation.billingDaysSum }"
          ></div>
        </div>
      </div>

      <div class="fx">
        <div class="block">
          <div class="block-left">近12月作废发票数量占比：</div>
          <div
            class="block-right"
            v-html-no="{
              action: invoiceInformation.taxVoidRateMonth12,
              unit: '%'
            }"
          ></div>
        </div>
        <div class="block">
          <div class="block-left">近12个月断票月数：</div>
          <div
            class="block-right"
            v-html-no="{ action: invoiceInformation.breakMonthsSum12 }"
          ></div>
        </div>
      </div>
      <div class="fx">
        <div class="block f5bg">
          <div class="block-left">近12月最长连续断票月数：</div>
          <div
            class="block-right"
            v-html-no="{
              action: `${invoiceInformation.breakMonthsSum}`
            }"
          ></div>
        </div>
        <div class="block f5bg">
          <div class="block-left">近12个月最大连续未开票间隔天数(销项)：</div>
          <div
            class="block-right"
            v-html-no="{
              action: invoiceInformation.taxOutputNotInvoicedMonth12
            }"
          ></div>
        </div>
      </div>
      <div class="fx">
        <div class="block ">
          <div class="block-left">近12个月月均开票金额同比增长率：</div>
          <div
            class="block-right"
            v-html-no="{
              action: `${invoiceInformation.taxAmountGrowthRateMonth12 }`,
              unit: '%'
            }"
          ></div>
        </div>
      </div>

      <div style="margin-top: 30px">
        <div style="font-weight: bold; color: #222222">开票对比分析</div>
        <el-table
          style="margin-top: 10px"
          :data="enterpriseMonthApplyList"
          border
        >
          <el-table-column
            align="center"
            prop="name"
            label="开票对比分析"
          ></el-table-column>
          <el-table-column
            align="center"
            v-for="month in monthList"
            :key="month"
            :label="monthLabel(month)"
          >
          <template slot-scope="scope">
              <div v-if="scope.row.name== '开票环比增长率' || scope.row.name == '开票同比增长率'" v-html-no="{ action: scope.row[month] , unit: '%'}"></div>
              <div v-else v-html-no="{ action: scope.row[month] }"></div>
             </template>
        </el-table-column>
        </el-table>
      </div>

      <div style="margin-top: 30px">
        <div style="font-weight: bold; color: #222222">近三年开票总金额</div>
        <div>
          <month-table
            style="margin-top: 20px"
            :tableList="invoiceInfoDataList"
          ></month-table>
        </div>
      </div>

      <div style="margin-top: 30px">
        <div style="font-weight: bold; color: #222222">近三年无票收入</div>
        <div>
          <month-table
            style="margin-top: 20px"
            :tableList="applySalesDataList"
          ></month-table>
        </div>
      </div>
    </div>

    <div class="canpanyer-box">
      <div class="title-msg">
        红冲金额
      </div>
      <div style="margin-top: 20px">
        <monthTable :table-list="redInvoiceInfoDataList"></monthTable>
      </div>
      <div class="title-msg">
        作废金额
      </div>
      <div style="margin-top: 20px">
        <monthTable :table-list="invoiceCountDataList"></monthTable>
      </div>
      <div class="title-msg">
        前十供应商信息
      </div>
      <div style="margin-top: 20px">
        <el-table :data="shopList" border>
          <el-table-column
            prop="sort"
            align="center"
            width="100"
            label="排名"
          ></el-table-column>
          <el-table-column
            align="center"
            prop="buySaleName"
            label="供应商名称"
          ></el-table-column>
          <el-table-column
            align="center"
            prop="sumAmount"
            label="采购额(元)"
          ></el-table-column>
          <el-table-column
            align="center"
            prop="amountRate"
            label="金额占比(%)"
          ></el-table-column>
          <el-table-column align="center" label="是否关联方">
            <template slot-scope="scope">
              {{ scope.row.relatedStatus ? "是" : "否" }}
            </template>
          </el-table-column>
        </el-table>
      </div>
      <div class="title-msg">
        前十供销售客户信息
      </div>
      <div style="margin-top: 20px">
        <el-table :data="customList" border>
          <el-table-column
            align="center"
            width="100"
            prop="sort"
            label="排名"
          ></el-table-column>
          <el-table-column
            align="center"
            prop="buySaleName"
            label="供应商名称"
          ></el-table-column>
          <el-table-column
            align="center"
            prop="sumAmount"
            label="采购额(元)"
          ></el-table-column>
          <el-table-column
            align="center"
            prop="amountRate"
            label="金额占比(%)"
          ></el-table-column>
          <el-table-column label="是否关联方" align="center">
            <template slot-scope="scope">
              {{ scope.row.relatedStatus ? "是" : "否" }}
            </template>
          </el-table-column>
        </el-table>
      </div>
    </div>
  </div>
</template>
<script>
import { monthUtil } from "@/utils/date";
import monthTable from "../../../../components/monthTable/index.vue";
import {
  getEnterpriseApplyAmountInfo,
  getgetEnterpriseInvoiceTopTen,
  getEnterpriseApplyAmount
} from "@/api/customer";
export default {
  name: "invoiceInformation",
  props: {
    detailInfo: {
      type: Object,
      default: () => {
        return {};
      }
    }
  },
  components: {
    monthTable
  },
  data() {
    return {
      invoiceInformation: {},
      paidTaxesDataList: [],
      redInvoiceInfoDataList: [],
      invoiceCountDataList: [],
      applySalesDataList: [],
      invoiceInfoDataList: [],
      showLoading: false,
      shopList: [],
      customList: [],
      type: 1,
      enterpriseMonthApplyList: [],
      monthList: []
    };
  },
  created() {
    this.getCmsMatchDetailInfo(this.detailInfo.id);
    this.getEnterpriseInvoiceTopTenInfo(this.detailInfo.id);
  },
  methods: {
    monthLabel(month) {
      switch (month) {
        case "month3":
          return "近3个月";
        case "month6":
          return "近6个月";
        case "month12":
          return "近12个月";
        case "month24":
          return "近24个月";
        default:
          return month;
      }
    },
    getEnterpriseInvoiceTopTenInfo(id) {
      getgetEnterpriseInvoiceTopTen(id).then(res => {
        this.shopList = res.data.paidTaxesDataList;
        this.customList = res.data.redInvoiceInfoDataList;
      });
    },
    funData(arr, type) {
      console.log(arr);
      let objMap = {};
      type == 1
        ? (objMap = {
            invoiceAmount: "开票金额",
            invoiceGrowthRate: "开票环比增长率",
            invoiceOverGrowthRate: "开票同比增长率",
            invoiceCount: "开票张数",
            invoiceMonthsNum: "开票月数",
            invoiceRedCountRate: "红冲发票张数占比",
            invoiceRedAmountRate: "红冲金额占比",
            customerUpNum: "上游客户数量",
            customerDownNum: "下游客户数量"
          })
        : (objMap = {
            applyAmount: "申报金额",
            applyGrowthRate: "申报环比增长率",
            applyOverGrowthRate: "申报同比增长率"
          });

      const data = arr.map(item => {
        return {
          month: item.month,
          ...item.taxEnterpriseApplyDate
        };
      });
      const result = Object.keys(objMap).map(key => ({
        name: objMap[key],
        ...data.reduce((acc, item) => {
          acc[`month${item.month}`] = null;
          return acc;
        }, {})
      }));
      data.forEach(item => {
        Object.entries(item).forEach(([key, value]) => {
          if (objMap[key]) {
            const resItem = result.find(r => r.name === objMap[key]);
            if (resItem) {
              resItem[`month${item.month}`] = value;
            }
          }
        });
      });
      let arrr = JSON.parse(JSON.stringify(result));
      const obj = delete arrr[0].name;
      this.monthList = Object.keys(arrr[0]);
      return result;
    },
    // 获取申报数据
    //获取开票数据
    getCmsMatchDetailInfo(id) {
      this.showLoading = true;
      getEnterpriseApplyAmountInfo(id)
        .then(res => {
          this.invoiceInformation = res.data;
          this.enterpriseMonthApplyList = this.funData(
            res.data.enterpriseMonthApplyList,
            1
          );
          const arr = [
            "redInvoiceInfoDataList",
            "invoiceCountDataList",
            "invoiceInfoDataList",
            "applySalesDataList"
          ];
          arr.forEach(item => {
            this[item] = res.data[item].map(v => {
              return {
                year: v.year,
                ...monthUtil(v.monthData),
                add: v.total
              };
            });
          });

          this.showLoading = false;
        })
        .catch(() => {
          this.showLoading = false;
        });
    }
  }
};
</script>

<style scoped lang="scss">
.f5bg {
  background: #f5f5f5;
}
.fx {
  display: flex;
  align-items: center;
  justify-content: space-between;
  width: 100%;
  .block {
    width: 48%;
    display: flex;
    align-items: center;
    max-height: 60px;
    padding: 18px 0;

    //line-height: 56px;
    .block-left {
      display: inline-block;
      //display: flex;
      text-align: right;
      width: 38%;
      //white-space: nowrap;
      color: #999;
    }
    .block-right {
      display: inline-block;
      //display: flex;
      line-height: 18px;
      color: #222;
      width: 60%;
    }
  }
}
.title-msg {
  margin-top: 20px;
  font-size: 18px;
  font-weight: 500;
  color: #222222;
}
</style>
